package com.cash.app.utils

import com.cash.app.constant.AppConstant.API_HOST_BACKUP
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException

object UrlContentUtils {

    private val TAG = "UrlContentUtils"
    private val client = OkHttpClient()

    /**
     * Read content from URL
     * @param url Target URL to read from
     * @return Content as string or null if failed
     */
    fun readUrlContent(url: String): String {
        return try {
            val request = Request.Builder().url(url).build()

            client.newCall(request).execute().use { response ->
                if (!response.isSuccessful) throw IOException("Unexpected response ${response.code}")
                val backupHost = response.body?.string()
                TLog.d("backupHost=$backupHost", TAG)
                backupHost ?: API_HOST_BACKUP
            }
        } catch (e: Exception) {
            e.printStackTrace()
            API_HOST_BACKUP
        }
    }
} 